﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Threading;

namespace Reporter.Controls
{
    public class PublishInfo : INotifyPropertyChanged
    {
        // three state : false = stage not started, null = stace in progress, true = stage finished
        private int _notebooksPublishedCount;
        private int _notebooksTotal;
        private int _imagesPublishedCount;
        private int _imagesTotal;
        private bool? _notebooksPublished;
        private bool? _imagesPublished;
        private string _ellapsedTime = "00:00:00";
        private DateTime _startingTime;
        private DispatcherTimer _timer;

        public string EllapsedTime
        {
            get { return _ellapsedTime; }
            set
            {
                if (this._ellapsedTime != value)
                {
                    this._ellapsedTime = value;
                    this.NotifyPropertyChanged("EllapsedTime");
                }
            }
        }

        public int NotebooksPublishedCount
        {
            get { return this._notebooksPublishedCount; }
            set
            {
                if (this._notebooksPublishedCount != value)
                {
                    this._notebooksPublishedCount = value;
                    this.NotifyPropertyChanged("NotebooksPublishedCount");
                }
            }
        }

        public int NotebooksTotal
        {
            get { return this._notebooksTotal; }
            set
            {
                if (this._notebooksTotal != value)
                {
                    this._notebooksTotal = value;
                    this.NotifyPropertyChanged("NotebooksTotal");
                }
            }
        }

        public int ImagesPublishedCount
        {
            get { return this._imagesPublishedCount; }
            set
            {
                if (this._imagesPublishedCount != value)
                {
                    this._imagesPublishedCount = value;
                    this.NotifyPropertyChanged("ImagesPublishedCount");
                }
            }
        }

        public int ImagesTotal
        {
            get { return this._imagesTotal; }
            set
            {
                if (this._imagesTotal != value)
                {
                    this._imagesTotal = value;
                    this.NotifyPropertyChanged("ImagesTotal");
                }
            }
        }

        public bool? NotebooksPublished
        {
            get { return this._notebooksPublished; }
            set
            {
                if (this._notebooksPublished != value)
                {
                    this._notebooksPublished = value;
                    this.NotifyPropertyChanged("NotebooksPublished");
                }
            }
        }

        public bool? ImagesPublished
        {
            get { return this._imagesPublished; }
            set
            {
                if (this._imagesPublished != value)
                {
                    this._imagesPublished = value;
                    this.NotifyPropertyChanged("ImagesPublished");
                }
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        protected void NotifyPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        public void StartTimer()
        {
            _timer = new DispatcherTimer();
            _timer.Tick += new EventHandler(Timer_Tick);
            _timer.Interval = TimeSpan.FromSeconds(1);

            _startingTime = DateTime.Now;
            _timer.Start();
        }

        void Timer_Tick(object sender, EventArgs e)
        {
            var time = DateTime.Now - _startingTime;
            var hours = (time.Hours < 10) ? string.Format("0{0}", time.Hours) : time.Hours.ToString();
            var minutes = (time.Minutes < 10) ? string.Format("0{0}", time.Minutes) : time.Minutes.ToString();
            var seconds = (time.Seconds < 10) ? string.Format("0{0}", time.Seconds) : time.Seconds.ToString();
            EllapsedTime = String.Format("{0}:{1}:{2}", hours, minutes, seconds);
        }

        public void StopTimer()
        {
            _timer.Stop();
        }


    }
}
